--[[
	Ward Prediction 1.0 by eXtragoZ
			
		It uses AllClass
	
	Features:
		- Prints in the chat the amount of wards purchased and used by allies and enemies
		- Indicates the position where may be the ward that use the enemy with circles and says the remaining duration
		- You can remove the marks pressing shift and double click in the circle
]]
--[[		Config		]]
local HK = 16 --shift
--[[		Code		]]
local lastclick = 0
local deletewards = 0
local countSightWard,lastcountSightWard,countVisionWard,lastcountVisionWard = {},{},{},{}
local itemslot = {ITEM_1,ITEM_2,ITEM_3,ITEM_4,ITEM_5,ITEM_6}
local colorteam = "#0095FF"
local unittraveled = {}
local MissTimer = {}
local MissSec = {}
local tick_heros = {}
local centerpos = {}
local possibleposlist = {}

function OnLoad()
	for i=1, heroManager.iCount do countSightWard[i],lastcountSightWard[i],countVisionWard[i],lastcountVisionWard[i] = 0,0,0,0 end
	PrintChat(" >> Ward Prediction 1.0 loaded!")
end
function OnTick()
	for i=1, heroManager.iCount do
		local champion = heroManager:GetHero(i)
		countSightWard[i] = 0
		countVisionWard[i] = 0
		for j=1, 6 do
			local item = champion:getItem(itemslot[j])
			if item ~= nil and item.id == 2044 then countSightWard[i] = countSightWard[i] + item.stacks end
			if item ~= nil and item.id == 2043 then countVisionWard[i] = countVisionWard[i] + item.stacks end
		end
		colorteam = (champion.team == myHero.team and "#0095FF" or "#FF0000")
		if lastcountSightWard[i] > countSightWard[i] then
			PrintChat("<font color='"..colorteam.."'>"..champion.charName.."</font><font color='#EEA111'> used "..lastcountSightWard[i]-countSightWard[i].." </font><font color='#199C33'>Sight Ward</font>")
			if unittraveled[i] ~= nil and champion.team ~= myHero.team then
				table.insert(possibleposlist, {x = (centerpos[i].x+champion.x)/2, y = (centerpos[i].y+champion.y)/2, z = (centerpos[i].z+champion.z)/2, wardtime = GetTickCount()+180000, warddistance = unittraveled[i]/2+680})
			end
		end
		if lastcountSightWard[i] < countSightWard[i] then
			PrintChat("<font color='"..colorteam.."'>"..champion.charName.."</font><font color='#FFFC00'> buy "..countSightWard[i]-lastcountSightWard[i].." </font><font color='#199C33'>Sight Ward</font>")
		end
		if lastcountVisionWard[i] > countVisionWard[i] then
			PrintChat("<font color='"..colorteam.."'>"..champion.charName.."</font><font color='#EEA111'> used "..lastcountVisionWard[i]-countVisionWard[i].." </font><font color='#C000FF'>Vision Ward</font>")
			if unittraveled[i] ~= nil and champion.team ~= myHero.team then
				table.insert(possibleposlist, {x = (centerpos[i].x+champion.x)/2, y = (centerpos[i].y+champion.y)/2, z = (centerpos[i].z+champion.z)/2, wardtime = GetTickCount()+180000, warddistance = unittraveled[i]/2+680})
			end
		end
		if lastcountVisionWard[i] < countVisionWard[i] then
			PrintChat("<font color='"..colorteam.."'>"..champion.charName.."</font><font color='#FFFC00'> buy "..countVisionWard[i]-lastcountVisionWard[i].." </font><font color='#C000FF'>Vision Ward</font>")
		end
		lastcountSightWard[i] = countSightWard[i]
		lastcountVisionWard[i] = countVisionWard[i]
	end
	for i,object in ipairs(possibleposlist) do
		if GetTickCount() > object.wardtime then
			table.remove(possibleposlist,i)
		elseif GetTickCount()-deletewards <= 50 then
			if GetDistance(object,mousePos)<=object.warddistance then
				table.remove(possibleposlist,i)
			end
		end
	end
	for i=1, heroManager.iCount do
		local heros = heroManager:GetHero(i)
		if heros.team ~= myHero.team and not heros.visible and not heros.dead then
			if tick_heros[i] == nil then tick_heros[i] = GetTickCount() end
			MissTimer[i] = GetTickCount() - tick_heros[i]			
			MissSec[i] =  MissTimer[i]/1000
			unittraveled[i] = heros.ms*MissSec[i]
		else
			tick_heros[i] = nil
			MissTimer[i] = nil
			MissSec[i] = 0
			unittraveled[i] = 0
		end
		centerpos[i] = {x = heros.x, y = heros.y, z = heros.z}
	end
end

function OnDraw()
	local order = 0
	for i,object in ipairs(possibleposlist) do
		if object.warddistance <= 10000 then
			DrawCircle(object.x, object.y, object.z, object.warddistance, 0xFF11FF)
		end
		if GetDistance(object,mousePos)<=object.warddistance then
			local cursor = GetCursorPos()
			DrawText("Possible ward "..timerText((object.wardtime-GetTickCount())/1000),16, cursor.x, cursor.y + 50+15*order, RGBA(255,100,0,255))
			order = order+1
		end
	end
end

function OnWndMsg(msg,key)
	if IsKeyDown(HK) then
		if msg == WM_LBUTTONUP then
			if GetTickCount()-lastclick <= 200 then
				deletewards = GetTickCount()
			end
			lastclick = GetTickCount()
		end
	end
end
